package com.swxc.smartorder.ui.activity

import android.os.Bundle
import android.support.v4.view.ViewPager
import com.swxc.smartorder.R
import com.swxc.smartorder.adapter.PreviewPagerAdapter
import com.swxc.smartorder.ui.base.BaseActivity
import com.zhihu.matisse.internal.ui.PreviewItemFragment
import kotlinx.android.synthetic.main.activity_preview.*


/**
 * 预览
 */
open class PreviewActivity : BaseActivity(), ViewPager.OnPageChangeListener {

    private var mPreviousPos = -1
    private var mAdapter: PreviewPagerAdapter? = null

    private var mImageList: MutableList<String>? = null
    private var mIndex: Int = 0

    override fun onPageScrollStateChanged(state: Int) {
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {
        val adapter = viewpager.adapter as PreviewPagerAdapter
        if (mPreviousPos != -1 && mPreviousPos != position) {
            (adapter.instantiateItem(viewpager, mPreviousPos) as PreviewItemFragment).resetView()

            val item = adapter.getMediaItem(position)

//            updateSize(item)
        }
        mPreviousPos = position
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_preview)

        initBundleData()
        initViewAndEvent()
    }

    private fun initBundleData() {
        mImageList = ArrayList()
        mImageList = intent.extras.getStringArrayList("images")
        mIndex = intent.extras.getInt("index")

    }

    private fun initViewAndEvent() {

        mAdapter = PreviewPagerAdapter(supportFragmentManager, PreviewPagerAdapter.OnPrimaryItemSetListener { })

        mAdapter?.addAll(mImageList)
        viewpager.adapter = mAdapter
        viewpager.currentItem = mIndex
        iv_back.setOnClickListener { finish() }
    }
}
